# This file handles building each task as sub-project.
#
# Each task becomes an independent project in which you can experiment.
#
# Tasks are added as separate sub-projects so as compilation errors in
# one task don't affect another task.

# 参考mylog的注释

cmake_minimum_required(VERSION 3.12)
project(mylib)

option(BUILD_EXAMPLE "if or not to build example targets" ON)

if(NOT LIBRARY_OUTPUT_PATH)
    set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/lib)
endif ()

if(NOT EXECUTABLE_OUTPUT_PATH)
    set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
endif ()

set(SRC_PATH ${PROJECT_SOURCE_DIR}/src)
set(SRC_MYLIB_PATH ${SRC_PATH}/${PROJECT_NAME})

set(PROJECT_SOURCES
        ${SRC_MYLIB_PATH}/my_lib.cpp
        ${SRC_MYLIB_PATH}/my_lib.h
)

set(CMAKE_CXX_STANDARD 14)

#此处使用EXIST_MYLOG判断是否需要将子目录添加到工程中，如果顶层定义了此变量，
#说明顶层存在改子项目，此处不需要增加子项目，使用顶层的子项目即可
if(NOT EXIST_MYLOG)
    add_subdirectory(pkt/mylog)
endif ()

add_library(${PROJECT_NAME} ${PROJECT_SOURCES})
target_compile_definitions(${PROJECT_NAME} PRIVATE MYLIB_LIBRARY)
target_link_libraries(${PROJECT_NAME} mylog)
target_include_directories(${PROJECT_NAME}
    PUBLIC
        $<INSTALL_INTERFACE:include>
        $<BUILD_INTERFACE:${SRC_PATH}>
)

install(TARGETS ${PROJECT_NAME}
        RUNTIME DESTINATION bin
        LIBRARY DESTINATION lib
        ARCHIVE DESTINATION lib
)

set(EXPORT_INCLUDES ${SRC_MYLIB_PATH}/my_lib.h)
install(FILES ${EXPORT_INCLUDES}
    DESTINATION include/${PROJECT_NAME}
)

if(BUILD_EXAMPLE)
    add_subdirectory(example)
endif ()
